from django.db.models import Q
from django.shortcuts import render,HttpResponse
from employee.models import *

# def register(request):
#     if request.method=='GET':
#         return render(request,'register.html')
#     else:
#         username=request.POST.get('user','')
#         password=request.POST.get('password','')
#         password2=request.POST.get('password2','')
#
#         if password != password2:
#             # return HttpResponse('两次输入密码不一致')
#             return render(request,'register.html',{'error':'两次密码不一致'})
#         user = Employee.objects.filter(email=username)
#         if user.count()>0:
#             # return HttpResponse('用户名被占用')
#             return render(request, 'register.html', {'error': '用户名被占用'})
#         user = Employee(email=username, password=password)
#         user.save()
#         return render(request,'login.html',{'succ':'注册成功，请登录'})

def login(request):
    if request.method == 'GET':
        return render(request,'login.html')
    else:
        username = request.POST.get('user', '')
        password = request.POST.get('password', '')

    users = Employee.objects.filter(Q(email=username, password=password)|Q(s_id=username, password=password))
    if users.count() > 0:
        if request.session.get('is_login',None):
            return render(request,'login.html',{'error':'不可以重复登录'})
        request.session['is_login']=True
        request.session['s_name']=users[0].s_name
        request.session['quanxian']=users[0].quanxian
        request.session['s_id']=users[0].s_id
        request.session['position']=users[0].position

        # return HttpResponse('登录成功')
        if users[0].quanxian =='1':
            return render(request,'main-admin.html')
        else:
            return render(request,'main-personal.html')
    else:
        # return HttpResponse('用户名或密码错误')
        return render(request, 'login.html', {'succ': '用户名或密码错误'})

def zhuxiao(request):
    if request.method=='GET':
        del request.session['is_login']
        del request.session['s_name']
        del request.session['position']
        del request.session['s_id']
        return render(request,'login.html')
